package com.javacrafting.mifos.domain;

import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class LoanProductRepositoryImpl extends HibernateDaoSupport implements LoanProductRepository {

	@SuppressWarnings("unchecked")
	public List<Product> findAllProducts() {
		return getHibernateTemplate().find("from ProductImpl");
	}

	public Product findById(final Short productId) {
		return (Product) getHibernateTemplate().get(ProductImpl.class,
				productId);
	}

	public LoanProduct findLoanProductById(Short productId) {

		return (LoanProduct) getSession().createCriteria(
				LoanProductImpl.class) //
				.setFetchMode("interestType", FetchMode.JOIN) //
				.setFetchMode("loanProductFunds", FetchMode.JOIN) //
				.add(Restrictions.idEq(productId)) //
				.uniqueResult();
	}
}
